<template>
  <div class="top-category" v-if="cate">
    <div class="container">
      <!-- 面包屑 -->
      <Transition name="fade-right" mode="out-in">
        <xtx-bread>
          <xtx-bread-item to="/">首页</xtx-bread-item>
          <xtx-bread-item :key="cate.id">{{ cate.name }}</xtx-bread-item>
        </xtx-bread>
      </Transition>
      <!-- 轮播图 -->
      <xtx-carousel
        :silders="silders"
        style="height: 500px"
        auto-play
      ></xtx-carousel>
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in cate.children" :key="item.id">
            <RouterLink :to="`/category/sub/${item.id}`">
              <img :src="item.picture" alt="" />
              <p>{{ item.name }}</p>
            </RouterLink>
          </li>
        </ul>
      </div>
      <!-- 不同分类商品 -->
      <div class="ref-goods" v-for="item in cate.children" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <p class="tag">{{ item.desc }}</p>
          <xtx-more :to="`/category/sub/${item.id}`"></xtx-more>
        </div>
        <div class="body">
          <goodsItem
            v-for="goods in item.goods"
            :key="goods.id"
            :goods="goods"
          ></goodsItem>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import { findBanner } from '@/api/home'
import { useRoute } from 'vue-router'
import { findTopCategory } from '@/api/category'
import goodsItem from '@/views/category/components/goods-item'
export default {
  name: 'TopCategory',
  components: { goodsItem },
  setup() {
    // 轮播图
    const silders = ref([])
    findBanner(2).then((data) => {
      silders.value = data.result
    })
    // 获取一级分类数据
    const route = useRoute()
    const cate = ref(null)
    // findTopCategory(route.params.id).then(({ result }) => {
    //   cate.value = result
    // })
    // 推荐商品
    const subList = ref([])
    const getSubList = () => {
      findTopCategory(route.params.id).then((data) => {
        subList.value = data.result.children
        // 手动添加desc字段
        data.result.children.forEach((result) => {
          result.desc = '温暖柔软，品质之选'
        })
        cate.value = data.result
      })
    }
    watch(
      () => route.params.id,
      (id) => {
        if (`/category/${id}` !== route.path) {
          return
        }
        id && getSubList()
      },
      { immediate: true }
    )
    return { silders, cate, subList }
  }
}
</script>
<style scoped lang="less">
.top-category {
  h3 {
    text-align: center;
    font-size: 28px;
    font-weight: normal;
    color: #666;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      flex-wrap: wrap;
      padding: 0 32px;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @color;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
</style>
